//
//  PageAccessoryViewController.swift
//  ShopIOS
//
//  Created by Apple on 2019/8/21.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit

class PageAccessoryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    @IBOutlet weak var lb_type: UILabel!
    @IBOutlet weak var lb_memo: UILabel!
    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var yes: UIView!
    @IBOutlet weak var no: UILabel!
    var OrderID:String!
    var AccessoryState:String!
    var dataSource=[mOrderAccessroyDetail]()
    init(OrderID:String){
        super.init(nibName: nil, bundle: nil)
        self.OrderID=OrderID
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        tableview.dataSource=self
        tableview.delegate=self
        tableview.separatorStyle = .none
        tableview.register(UINib(nibName: "AccessoryTableViewCell", bundle: nil), forCellReuseIdentifier: "re")
        getOrderDetail()
    }
    func getOrderDetail(){
        let d = ["OrderID":OrderID]as[String:String]
        AlamofireHelper.post(url: GetOrderInfo, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            let detail=mXgyOrderDetail.init(json: res["Data"])
            ss.lb_memo.text=detail.AccessoryMemo
            ss.lb_type.text=detail.AccessorySequencyStr
            ss.dataSource=detail.OrderAccessroyDetail
            if ss.dataSource.count>0{
                ss.yes.isHidden=false
                ss.no.isHidden=true
            }else{
                ss.yes.isHidden=true
                ss.no.isHidden=false
            }
            ss.tableview.reloadData()
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
    func approveBeyondMoney(){
        let d = ["OrderID":OrderID,"BeyondState":AccessoryState]as[String:String]
        AlamofireHelper.post(url: ApproveBeyondMoney, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            if res["Data"]["Item1"].boolValue{
                HUD.showText("审核成功")
                ss.getOrderDetail()
            }else{
                HUD.showText("审核失败")
            }
            
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableview.dequeueReusableCell(withIdentifier: "re")as!AccessoryTableViewCell
        cell.selectionStyle = .none
        let item=dataSource[indexPath.row]
        cell.btn_reject.layer.cornerRadius=5
        cell.btn_ok.layer.cornerRadius=5
        cell.lb_name.text=item.FAccessoryName
        cell.lb_count.text="￥\(item.Price)/\(item.Quantity)个"
        cell.btn_reject.addTarget(self, action: #selector(reject(sender:)), for: UIControl.Event.touchUpInside)
        cell.btn_ok.addTarget(self, action: #selector(ok(sender:)), for: UIControl.Event.touchUpInside)
        cell.btn_reject.tag = indexPath.row
        cell.btn_ok.tag = indexPath.row
        return cell
    }
    @objc func reject(sender:UIButton){
        let item=dataSource[sender.tag]
        
    }
    @objc func ok(sender:UIButton){
        let item=dataSource[sender.tag]
        
    }
}
